

using JuMP, KNITRO, Ipopt, JLD, MAT, Roots, MathProgBase, Gadfly

println("Starting: CES, z_f.")

# Get more workers
workers = addprocs(2)

# make sure that we're using the right libraries
@everywhere using JuMP, KNITRO, Ipopt, JLD, MAT, Roots, MathProgBase, Gadfly

# do all prep work on every worker
@everywhere include("cenf.jl")

@everywhere begin

  cenf.readData()

  # set seed, so that results can be replicated
  # my birthday
  rng = MersenneTwister(16091985)

  const CONST_MULTISTARTTRIES = 10

  ms_fvals=zeros(CONST_MULTISTARTTRIES,1);
  ms_β=zeros(CONST_MULTISTARTTRIES,2);
  ms_η=zeros(CONST_MULTISTARTTRIES,2);
  ms_γ=zeros(CONST_MULTISTARTTRIES,35*35);
  ms_logT=zeros(CONST_MULTISTARTTRIES,109*35);
  ms_logS=zeros(CONST_MULTISTARTTRIES,109*35);


  startβvec = Array(Array{Float64,1},CONST_MULTISTARTTRIES);
  startηvec = Array(Array{Float64,1},CONST_MULTISTARTTRIES);

  # generate starting values that satisfy constraint on beta
  for multiiter=1:CONST_MULTISTARTTRIES

      bDone = false
      while bDone==false

        startβvec[multiiter]=vec(rand(rng,Float64,1,2))
        startηvec[multiiter]=vec(1000.*rand(rng,Float64,1,2))

        # this is the constraint on the β
        if (startβvec[multiiter][1]+startβvec[multiiter][2]<=1)
          bDone=true
        end
      end

  end

end

# do the search in parallel
# syntax:  pmap((a1,a2)->f(a1,a2),{"a","b","c"},{2,1,3})
@time @sync outarray = pmap((startβ,startη)->cenf.solveCESWithρθ(2.0,startβ,startη),startβvec,startηvec)

save("$(root)/output/ms_ces_f_output_rhothetafixed.jld", "outarray", outarray)

rmprocs(workers)
